package com.sy.controller;

import com.sy.dto.JsonResult;
import com.sy.entity.CCollection;
import com.sy.entity.Cartoon;
import com.sy.entity.Media;
import com.sy.entity.VCollection;
import com.sy.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author King
 * @Date 2021/1/18 10:09
 */
@RestController
@RequestMapping("collection")
public class CollectionController {

    @Autowired
    private CollectionService service;

    /**
     * 根据用户id查询其收藏的漫画
     *
     * @return
     */
    @RequestMapping(value = "cartoon/{userId}", method = RequestMethod.GET)
    public JsonResult cartoonCollection(@PathVariable Integer userId) {
        JsonResult result = new JsonResult();
        List<Cartoon> cartCollection = service.getCartCollection(userId);
        if (cartCollection.size() == 0) {
            result.setData(null);
        } else {
            result.setData(cartCollection);
        }
        return result;
    }

    /**
     * 根据用户id查询其收藏的视频
     *
     * @return
     */
    @RequestMapping(value = "media/{userId}", method = RequestMethod.GET)
    public JsonResult mediaCollection(@PathVariable Integer userId) {
        JsonResult result = new JsonResult();
        List<Media> mediaCollection = service.getMediaCollection(userId);
        if (mediaCollection.size() == 0){
            result.setData(null);
        }else {
            result.setData(mediaCollection);
        }
        return result;
    }

    /**
     * 根据用户id删除漫画
     *
     * @param userId
     * @param req
     * @return
     */
    @RequestMapping(value = "{userId}/cartoon", method = RequestMethod.GET)
    public JsonResult delCollection1(@PathVariable("userId") Integer userId, HttpServletRequest req) {
        JsonResult result = new JsonResult();
        Integer cId = Integer.parseInt(req.getParameter("cId"));
//        System.out.println("删除的uid"+userId);
//        System.out.println("删除的cid"+cId);
        int i = service.delCollectionCartoon(userId, cId);
        if (i > 0) {
            result.setCode(204);
            result.setErrMsg("取消收藏成功");
        } else {
            result.setCode(400);
            result.setErrMsg("取消收藏失败");
        }
        return result;
    }

    /**
     * 根据用户id删除视频
     *
     * @param userId
     * @param req
     * @return
     */
    @RequestMapping(value = "{userId}/media", method = RequestMethod.GET)
    public JsonResult delCollection2(@PathVariable("userId") Integer userId, HttpServletRequest req) {
        JsonResult result = new JsonResult();
        int vId = Integer.parseInt(req.getParameter("vId"));
        System.out.println("删除的uid" + userId);
        System.out.println("删除的vid" + vId);
        int i = service.delCollectionMedia(userId, vId);
        if (i > 0) {
            result.setCode(204);
            result.setErrMsg("取消收藏成功");
        } else {
            result.setCode(400);
            result.setErrMsg("取消收藏失败");
        }
        return result;
    }

    /**
     * 根据用户id收藏漫画
     *
     * @param cId
     * @param req
     * @return
     */
    @RequestMapping(value = "{cId}", method = RequestMethod.GET, params = "userId")
    public JsonResult insertCartoon(@PathVariable("cId") Integer cId, HttpServletRequest req) {
        JsonResult result = new JsonResult();
        System.out.println("新增的漫画id" + cId);
        String userId = req.getParameter("userId");
        CCollection cCollection = service.selectCartoonCollection(Integer.parseInt(userId), cId);
        if (null != cCollection) {
            result.setErrMsg("该漫画已经收藏，无需再次收藏");
            return result;
        } else {
            int i = service.insertCartoonCollection(Integer.parseInt(userId), cId);
            if (i > 0) {
                result.setErrMsg("收藏成功");
            } else {
                result.setErrMsg("收藏失败");
            }
            return result;
        }
    }

    /**
     * 根据用户id收藏视频
     *
     * @param vId
     * @param req
     * @return
     */
    @RequestMapping(value = "media/{vId}", method = RequestMethod.GET, params = "userId")
    public JsonResult insertMedia(@PathVariable("vId") Integer vId, HttpServletRequest req) {
        JsonResult result = new JsonResult();
        System.out.println("新增的视频id" + vId);
        String userId = req.getParameter("userId");
        VCollection vCollection = service.selectMediaCollection(Integer.parseInt(userId), vId);
        if (null != vCollection) {
            result.setErrMsg("该视频已经收藏，无需再次收藏");
            return result;
        } else {
            int i = service.insertMediaCollection(Integer.parseInt(userId), vId);
            if (i > 0) {
                result.setErrMsg("收藏成功");
            } else {
                result.setErrMsg("收藏失败");
            }
            return result;
        }
    }
}